package com.solo.eggsolo.web.interceptor;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import com.solo.eggsolo.util.JwtUtil;
/**
 * 
 * @author Yu
 *
 */
@Component
public class JwtInterceptor implements HandlerInterceptor {
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
     // 1.如果接收到请求，预检请求，浏览器发送

		String token = request.getHeader("Authorization");
		if (token == null) {
			throw new ServletException("用户未登录");

		}
		try {
			if (JwtUtil.checkSign(token)) {
				return true;
			};

		} catch (Exception e) {
			e.printStackTrace(); // 打印token验证失败的原因
			throw new ServletException("token无效，请重新登录");
		}
		// 通过上述验证，通过拦截
		return true;
	}
}
